package spring.springbucks.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;
import spring.springbucks.controller.request.NewOrderForm;
import spring.springbucks.controller.request.OrderStateForm;
import spring.springbucks.enums.OrderStateEnum;
import spring.springbucks.integration.Barista;
import spring.springentity.entity.table.CoffeeEntity;
import spring.springentity.entity.table.OrderCoffeeEntity;
import spring.springentity.entity.table.OrderEntity;
import spring.springentity.repository.CoffeeRepository;
import spring.springentity.repository.OrderCoffeeRepository;
import spring.springentity.repository.OrderRepository;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class CoffeeOrderController {

  @Autowired
  private OrderCoffeeRepository orderCoffeeRepository;

  @Autowired
  private OrderRepository orderRepository;

  @Autowired
  private CoffeeRepository coffeeService;

  @Autowired
  private Barista barista;

  @Autowired
  RabbitTemplate rabbitTemplate;

  @GetMapping("/{id}")
  public OrderEntity getOrder(@PathVariable("id") Long id) {
      OrderEntity order = null;

    order = orderRepository.getById(id);
    log.info("Get Order: {}", order);

    return order;
  }

  @PostMapping(path = "/", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  @ResponseStatus(HttpStatus.CREATED)
  public NewOrderForm create(@RequestBody NewOrderForm newOrder) {
    log.info("Receive new Order {}", newOrder);

    List<CoffeeEntity> coffeeList = coffeeService.list(new QueryWrapper<CoffeeEntity>().in("NAME", newOrder.getItems()));

    OrderEntity order = new OrderEntity();
    order.setCustomer(newOrder.getCustomer());
    order.setState(OrderStateEnum.INIT.getValue());
    orderRepository.save(order);
    List<OrderCoffeeEntity> orderCoffeeEntityList = new ArrayList<>();
    coffeeList.forEach(coffee -> {
      OrderCoffeeEntity orderCoffee = new OrderCoffeeEntity();
      orderCoffee.setCoffeeOrderId(order.getId());
      orderCoffee.setItemsId(coffee.getId());
    });
    orderCoffeeRepository.saveBatch(orderCoffeeEntityList);

    barista.newOrders().send(MessageBuilder.withPayload(order.getId()).build());
      newOrder.setId(order.getId());
    return newOrder;
  }

  @PutMapping("/{id}")
  public OrderEntity updateState(@PathVariable("id") Long id, @RequestBody OrderStateForm orderState) {
    log.info("Update order state: {}", orderState);
    OrderEntity order = orderRepository.getById(id);
    order.setState(orderState.getState().getValue());
    orderRepository.updateById(order);
    return order;
  }
}
